package com.xtool.livewallpaper.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Outline
import android.graphics.Point
import android.util.DisplayMetrics
import android.view.Display
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewOutlineProvider
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import java.lang.reflect.Method


/**
 *
 * @Author: Ricky
 * @Date: 2021/1/20
 */
object UIHelper {
    val screenWidth: Int
        get() {
            val activity = ActivityStackManager.currentActivity
            val resources: Resources? = activity?.resources
            val dm: DisplayMetrics? = resources?.displayMetrics
            return dm?.widthPixels ?: Resources.getSystem().displayMetrics.widthPixels
        }

    val screenHeight: Int
        get() {
            val activity = ActivityStackManager.currentActivity
            val resources: Resources? = activity?.resources
            val dm: DisplayMetrics? = resources?.displayMetrics
            return dm?.heightPixels ?: Resources.getSystem().displayMetrics.heightPixels
        }
    val dpi: Int
        get() {
            val activity = ActivityStackManager.currentActivity
            val dm = DisplayMetrics()
            activity?.windowManager?.defaultDisplay?.getMetrics(dm)
            return dm.densityDpi
        }

    val refreshRate: Int
        get() {
            val activity = ActivityStackManager.currentActivity
            val display: Display? = activity?.windowManager?.defaultDisplay
            return display?.refreshRate?.toInt() ?: 0
        }

    @SuppressLint("PrivateApi")
    private fun getNavBarOverride(): String? {
        var sNavBarOverride: String? = null
        try {
            val c = Class.forName("android.os.SystemProperties")
            val m: Method = c.getDeclaredMethod("get", String::class.java)
            m.isAccessible = true
            sNavBarOverride = m.invoke(null, "qemu.hw.mainkeys") as? String
        } catch (e: Throwable) {
        }
        return sNavBarOverride
    }

    fun hasNavBar(): Boolean {
        val res: Resources = MainApplication.gContext.resources
        val resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android")
        return if (resourceId != 0) {
            var hasNav = res.getBoolean(resourceId)
            val sNavBarOverride: String? = getNavBarOverride()
            if ("1" == sNavBarOverride) {
                hasNav = false
            } else if ("0" == sNavBarOverride) {
                hasNav = true
            }
            hasNav
        } else {
            !ViewConfiguration.get(MainApplication.gContext).hasPermanentMenuKey()
        }
    }

    @SuppressLint("InternalInsetResource")
    fun getNavigationBarHeight(): Int {
        var result = 0
        if (hasNavBar()) {
            val res: Resources = MainApplication.gContext.resources
            val resourceId: Int = res.getIdentifier("navigation_bar_height", "dimen", "android")
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId)
            }
        }
        return result
    }

    fun getFullScreenSize(activity: Activity): Point {
        val size = Point()
        activity.windowManager.defaultDisplay.getRealSize(size)
        return size
    }

    fun getStatusBarHeight(): Int {
        val result = 0
        try {
            val resourceId =
                Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                val sizeOne = Resources.getSystem().getDimensionPixelSize(resourceId)
                val sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId)
                return if (sizeTwo >= sizeOne) {
                    sizeTwo
                } else {
                    val densityOne = Resources.getSystem().displayMetrics.density
                    val densityTwo = Resources.getSystem().displayMetrics.density
                    val f = sizeOne * densityTwo / densityOne
                    (if (f >= 0) f + 0.5f else f - 0.5f).toInt()
                }
            }
        } catch (e: Resources.NotFoundException) {
            return 0
        }
        return result
    }
}

val Int.dp: Int
    get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt()

val Float.dp: Float
    get() = this * Resources.getSystem().displayMetrics.density + 0.5f

fun getString(@StringRes res: Int, context: Context = MainApplication.gContext): String {
    return context.getString(res)
}


fun getColorCompat(@ColorRes res: Int, context: Context = MainApplication.gContext): Int {
    return ContextCompat.getColor(context, res)
}
fun View.visible() {
    visibility = View.VISIBLE
}

fun View.invisible() {
    visibility = View.INVISIBLE
}

fun View.gone() {
    visibility = View.GONE
}

fun View.visually(flag: Boolean) {
    if (flag) visible() else gone()
}

fun View.round(radius: Float) {
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            outline.setRoundRect(0, 0, view.width, view.height, radius)
        }
    }
    clipToOutline = true
}
